package com.relytosoft.mizar.analyze.algorithm.face.bitmain.service.impl;

import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.analyze.algorithm.base.feign.bitman.FaceFeignService;
import com.relytosoft.mizar.analyze.algorithm.base.service.PersonService;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.*;
import com.relytosoft.mizar.analyze.common.utils.BitmanRequestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PersonServiceImpl
 *
 * @Author SunLi
 * @Date: 2022/5/16 19:16
 * @Version 1.0
 */
@Service
public class PersonServiceImpl implements PersonService {
    @Resource
    private FaceFeignService faceFeignService;

    @Override
    public String createGroup(FaceGroupReqDto faceGroupReqDto) {
        Map<String, Object> bodys = new HashMap<>(2);
        bodys.put("Volume", faceGroupReqDto.getVolume());
        bodys.put("GroupType", 0);
        String strBodys = JSON.toJSONString(bodys);
        String result = BitmanRequestUtil.handlerResponse(faceFeignService.createGroup(strBodys));
        return JSON.parseObject(result).getString("GroupId");
    }

    @Override
    public ResponseResult deleteGroup(FaceGroupReqDto faceGroupReqDto) {
        Map<String, Object> bodys = new HashMap<>(1);
        bodys.put("GroupIds", faceGroupReqDto.getGroupIds());
        String strBodys = JSON.toJSONString(bodys);
        BitmanRequestUtil.handlerResponse(faceFeignService.deleteGroup(strBodys));
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult createPerson(FacePersonReqDto facePersonReqDto) {
        Map<String, Object> bodys = new HashMap<>(2);
        bodys.put("Image", facePersonReqDto.getImage());
        bodys.put("GroupIds", facePersonReqDto.getGroupIds());
        String strBodys = JSON.toJSONString(bodys);
        String result = BitmanRequestUtil.handlerResponse(faceFeignService.createPerson(strBodys));
        List<FacePersonResDto> jsonObject = JSON.parseArray(result, FacePersonResDto.class);
        Map<String, Object> response = new HashMap<>(2);
        response.put("faceId", facePersonReqDto.getFaceId());
        response.put("personInfos", jsonObject);
        return ResponseResult.ok(response);
    }

    @Override
    public ResponseResult deletePerson(FacePersonReqDto facePersonReqDto) {
        Map<String, Object> bodys = new HashMap<>(2);
        bodys.put("PersonId", facePersonReqDto.getPersonId());
        bodys.put("GroupId", facePersonReqDto.getGroupId());
        String strBodys = JSON.toJSONString(bodys);
        BitmanRequestUtil.handlerResponse(faceFeignService.deletePerson(strBodys));
        return ResponseResult.ok();
    }

    @Override
    public String searchPerson(FaceSearchReqDto faceSearchReqDto) {
        Map<String, Object> bodys = new HashMap<>(4);
        bodys.put("Feature", faceSearchReqDto.getFeature());
        bodys.put("GroupIds", faceSearchReqDto.getGroupIds());
        bodys.put("Threshold", faceSearchReqDto.getThreshold());
        bodys.put("Top", faceSearchReqDto.getTop());
        String strBodys = JSON.toJSONString(bodys);
        return BitmanRequestUtil.handlerResponse(faceFeignService.searchPerson(strBodys));
    }

    @Override
    public String createDisposition(DispositionReqDto dispositionReqDto, String uploadUrl) {

        ArrayList<Map<String, Object>> arrBodys = new ArrayList<>();

        Map<String, Object> bodys = new HashMap<>(10);

        Map<String, Object> source = new HashMap<>();
        source.put("SrcID", dispositionReqDto.getDeviceId());
        bodys.put("Source", source);

        Map<String, Object> report = new HashMap<>();
        report.put("AlarmUploadUrl", uploadUrl);
        bodys.put("Report", report);

        bodys.put("StartTime", dispositionReqDto.getStartTime());
        bodys.put("EndTime", dispositionReqDto.getEndTime());

        Map<String, Object> executeControlData = new HashMap<>();
        executeControlData.put("Type", 1);
        if (dispositionReqDto.getType() == 1) {
            //图片布控
            Map<String, Object> imageFeature = new HashMap<>();
            imageFeature.put("FeatureData", dispositionReqDto.getFeatureData());
            imageFeature.put("FeatureType", 1);
            imageFeature.put("Top", 1);
            imageFeature.put("Threshold", dispositionReqDto.getThreshold());
            executeControlData.put("ImageFeature", imageFeature);
        } else if (dispositionReqDto.getType() == 2) {
            //人脸库布控
            ArrayList<Map<String, Object>> faceGroups = new ArrayList<>();
            for (String groupId :
                    dispositionReqDto.getGroupIds()) {
                Map<String, Object> group = new HashMap<>();
                group.put("FaceGroupID", groupId);
                group.put("Threshold", dispositionReqDto.getThreshold());
                group.put("Top", 1);
                faceGroups.add(group);
            }
            executeControlData.put("FaceGroups", faceGroups);
        }
        bodys.put("ExecuteControlData", executeControlData);
        arrBodys.add(bodys);
        String strBodys = JSON.toJSONString(arrBodys);
        String result = BitmanRequestUtil.handlerResponse(faceFeignService.createExecuteControl(strBodys));
        return JSON.parseObject(result).getString("ExecuteControlID");
    }

    @Override
    public ResponseResult deleteDisposition(String dispositionId) {
        Map<String, Object> bodys = new HashMap<>(1);
        bodys.put("ExecuteControlID", dispositionId);
        String strBodys = JSON.toJSONString(bodys);
        BitmanRequestUtil.handlerResponse(faceFeignService.deleteExecuteControl(strBodys));
        return ResponseResult.ok();
    }
}
